// paging module
var T = {};
T.paging = function(data){	
	if(data.ns <= data.onPage) return '';
	var onPage = data.onPage>0 ? data.onPage : 1; // количество записей на одной странице
	var activePage = data.activePage>0 ? Number(data.activePage) : 0;
	var pages=Math.ceil(data.ns/onPage); // общее количество страниц	
	function findStartLink(activePage){			
		for(var i=activePage; i>(activePage-12); i--){
			if((RegExp('0$')).test(i)){
				return i;	
			}					
		}
	}	
	var p = [];
	this.tag = function(a){
	var r ='';
	for(var i=0; i<a.length; i++){
		if(a[i][0]!=activePage){
			r+='<a href="'+DOMAIN+'/page/'+a[i][0]+'" name="'+a[i][0]+'" class="pagination" title="'+a[i][1]+'">'+a[i][2]+'</a>';
		}
		else{
			r+='<span>'+ (a[i][0]+1) +'</span>';
		}
	}
	return r;
	}		
	var startLink = findStartLink(activePage);	
	if (activePage) { 		 
		p.push([0,'Первая','««'], [activePage-1,'Предыдующая','«']);				
	}	
	for (var i=startLink; i<Math.min(startLink+10, pages); i++) {			
		p.push([i,'',i+1]);		
	}
	if (activePage<(pages-1)) {			 
		 p.push([activePage+1,'Следующая','»'], [pages-1,'Последняя','»»']);			 
	}		
	return this.tag(p);		
};

T.paging.keyboardBinds = function(el, c, f){
	$(el, c).focus(function(){				
		$(this).unbind('keypress').keypress(function(e){						
			var page =  parseInt($('.pages-box span',this).text());
			
			switch (e.keyCode) {
			  case 37:
			  	(function(){
					page = (page-2)<0 ? 0 : page;
					f.call(this,(page-2));					
				})();
			  break;
			  case 39:f.call(this, page);break;
			}
		}).addClass('focus');				
	}).blur(function(){
		$(this).removeClass('focus');	
	});
}





var Room = {	
	ctx:document,
	JSON:function(url, data, callback){		
		($.isFunction(data)) ? (callback = data, data = undefined) : '';		
		$('.main-loader', this.ctx).show();
		$.ajax({url: url, dataType: "json",	data: data, context:this,
			success: function(data, status) {		
				$('.main-loader', this.ctx).hide(); 
				if (data.error=='auth') return  Room.popup(T.tmpl("#eAuthTpl",{}));
				($.isFunction(callback)) ? callback(data) : '';															
			}
		});			
	},
	p:{},
	popup:function(t,d){		
		(Room.p.modal) ? Room.p.replace(t) : (Room.p='', Room.p = new Popup(t,d));
	},
	router:function(){
			
	}
	
};

var WJ = {};

WJ.Slides = (function(self, $){	
	self.allTrends = [];
	self.quotes = [];
	
	self.highLight = function(el){
		$('li','.stright').removeClass('on');
		$(el).parents('li').addClass('on');
	}
	
	self.showImage = function(symbol,el){		
		self.highLight(el);
		$('.loader',"#chart1").show();
		Room.JSON(DOMAIN+'/gallery/get/'+symbol,function(data){
			if(data.success){
				
				$('img',"#chart1").fadeOut('',function(){
					$(this).attr('src',DOMAIN+'/pub/gallery/full_'+data.success.image[0].id+'.jpg').fadeIn();								   
				});							   
				$('.title',"#chart1").html(data.success.image[0].name);
				$('.loader',"#chart1").hide();		
			}											 
		});		
	}
	
	self.start = function(){
		$('a:first','.stright').click();
		self.loadPhotos(0);
		return self;
	};
	
	self.randsort = function(c) {
		var o = new Array();
		for (var i = 0; i < c; i++) {
			var n = Math.floor(Math.random()*c);
			if( jQuery.inArray(n, o) > 0 ) --i;
			else o.push(n);
		}
		return o;
	}
	
	self.preload = function(el,ctx){
		//$('img',ctx).hide().load(function () {
		//	$(this).fadeIn(1000);
		//});	
		var e = $(el,ctx);
		var c = e.size();
		var r = self.randsort(c);
		$(el,ctx).each(function(i) {
			var e = $(this);			
			setTimeout(function(){
				e.addClass('preview');				
			}, r[i]*500);
		});		
	}
	
	
	self.loadPhotos = function(page){
			Room.JSON(DOMAIN+'/gallery/items/'+(page || 0), function(data){			
				
				$('.stright').html(data.success.html);
				//$('img','.stright').load(function(){$(this).addClass('preview');});
				self.preload("img",'.stright');
				//self.preload('.stright');							
				
				$('.pages-box').html(T.paging(data.success.logPaging));
				$('.pages-box a').click(function(){
					self.loadPhotos(this.name);
					return false;
				});
				(data.success.logPaging.ns <= data.success.logPaging.onPage) ? 
				$('.pages-box').hide() : 
				$('.pages-box').show();
				
			});
	}
	return self;
})(WJ.Slides || {}, jQuery);



$(document).ready(function() {		
	//T.loadTpls();		
	WJ.Slides.start().preload('img',document);
	
	
});